<?php
namespace app\index\controller;

class Supply extends Common
{
    public function index()
    {   $keyword=input('keyword');
   
       //echo $keyword;
        if(empty($keyword)){
            $products=db('product')->where(array('promote'=>0))->order('id desc')->paginate(30);
            $this->assign('products',$products);

            $promotes=db('product')->where(array('promote'=>1))->order('time desc')->paginate(30);
            $this->assign('promotes',$promotes);
        }else{
            //$products=db('product')->where('name','like','%'.$keyword.'%')->select()->order('id desc')->paginate(30);
            $products=db('product')->where('name','like','%'.$keyword.'%')->order('id desc')->paginate(30);
            //print_r($products);
            $this->assign('products',$products);

            $promotes=array();
            $this->assign('promotes',$promotes);
        }
       
       
        return view();
    }


    public function category(){
        $cateid=input('id');
        $this->assign('cateid',$cateid);
        if(empty($cateid)){
            $products=db('product')->field('*')->order('time desc')->paginate(30);
            $this->assign('products',$products);
            $promotes=db('product')->field('*')->where(array('promote'=>1))->order('time desc')->paginate(30);
            $this->assign('promotes',$promotes);
        }else{
            $products=db('product')->field('*')->where(array('cateid'=>$cateid,'promote'=>0))->order('time desc')->paginate(30);
            $this->assign('products',$products);

            $promotes=db('product')->field('*')->where(array('cateid'=>$cateid,'promote'=>1))->order('time desc')->paginate(30);
            $this->assign('promotes',$promotes);
        }

        $num=count($products);
        $this->assign("num",$num);

        




        return view();
    }


    public function detail(){
        $id=input('id');
        $product=db('product')->where(array('id'=>$id))->find();
         $cityname=db('tree')->where(array('id'=>$product['city']))->find();
          $townname=db('tree')->where(array('id'=>$product['town']))->find();
        $provincename=db('tree')->where(array('id'=>$product['province']))->find();
        $product['cityname']=$cityname['name'];
        $product['townname']=$townname['name'];
        $product['provincename']=$provincename['name'];
        $procate=db('procate')->where(array('id'=>$product['cateid']))->find();
        $this->assign('product',$product);
        $this->assign('procate',$procate);
        $jxs=db('product')->where(array('zan'=>2))->order('time DESC limit 10')->select();
        $this->assign('jxs',$jxs);
        $promotes=db('product')->where(array('cateid'=>$procate['id']))->order('time DESC limit 30')->select();
        $this->assign('promotes',$promotes);
        return view();
    }


   
}
